﻿using Dyao.Book.Entity;
using Dyao.Book.Repository.Abstractions;
using System.Linq.Expressions;

namespace Dyao.Book.Repository.Repositories
{
    using Foundation.Replacement;

    public class BookCatalogueContentRepository : DataRepository<BookCatalogueContentEntity>, IBookCatalogueContentRepository
    {
        /// <inheritdoc cref="IBookCatalogueContentRepository.BulkInsertAsync"/>
        public override Task BulkInsertAsync(IEnumerable<BookCatalogueContentEntity> entities, CancellationToken cancellationToken)
        {
            return base.BulkInsertAsync(entities, cancellationToken);
        }

        /// <inheritdoc cref="IBookCatalogueContentRepository.BulkDeleteAsync"/>
        public Task BulkDeleteAsync(IEnumerable<BookCatalogueContentEntity> entities, Expression<Func<BookCatalogueContentEntity, object>> keyExpression, CancellationToken cancellationToken = default)
        {
            return base.BulkDeleteAsync(entities, op =>
            {
                op.ColumnPrimaryKeyExpression = keyExpression;
            }, cancellationToken);
        }


        /// <inheritdoc cref="IBookCatalogueContentRepository.GetListByChapterIdAsync"/>
        public async Task<List<T>> GetListByChapterIdAsync<T>(string chapterId, Expression<Func<BookCatalogueContentEntity, T>> selector, CancellationToken cancellationToken)
        {
            var query = this.GetQueryable()
                .Where(bc => bc.BookCatalogueId == chapterId)
                .OrderBy(bc => bc.Seq)
                .Select(selector);

            return await query.ToListAsync(cancellationToken);
        }
    }
}
